#ifndef CONFIG_VIEW_H
#define CONFIG_VIEW_H

#include <QWidget>
#include <QMenu>
#include <QAction>
#include <QStyledItemDelegate>
#include <QPainter>
#include <QTextDocument>
#include <QComboBox>
#include "state_info.h"
#include "search_tree_widget.h"
#include "alarm_rule.h"
#include "html_delegate.h"
#include <vector>

namespace Ui
{
    class ConfigView;
}

class QTreeWidgetItem;

class ConfigView : public QWidget
{
    Q_OBJECT

public:
    explicit ConfigView(SearchTreeWidget *search_tree, QWidget *parent = nullptr);
    ~ConfigView();
protected:
    void showEvent(QShowEvent *event) override;

    void hideEvent(QHideEvent *event) override;

private slots:
    /**
     * @brief 处理网络消息
     * @param method 消息方法
     * @param body 消息内容
     */
    void onNetworkMessageReceived(const QString &method, const QString &body);

    /**
     * @brief 处理生成告警策略按钮点击
     */
    void onGenerateAlarmStrategyButtonClicked();

    /**
     * @brief 处理搜索树形控件项点击
     * @param state_info 点击的项
     */
    void onSearchTreeWidgetItemClicked(StateInfo state_info);

    /**
     * @brief 处理删除告警策略按钮点击
     * @param row 要删除的行号
     */
    void onDeleteAlarmStrategyButtonClicked();

    /**
     * @brief 处理一键下装按钮点击
     */
    void onDownloadButtonClicked();

    /**
     * @brief 处理新增节点请求
     * @param pid 父节点ID
     * @param id 节点ID
     */
    void onAddNodeRequested(int pid, int id);

private:
    /**
     * @brief 加载告警规则
     */
    void loadAlarmRules();

    /**
     * @brief 保存告警规则到服务器
     */
    void saveAlarmRulesToServer();

    /**
     * @brief 获取告警级别对应的颜色
     * @param level 告警级别
     * @return 颜色代码
     */
    QString getAlarmLevelColor(const QString &level);

    /** 
     * @brief 渲染告警规则到表格
     */
    void renderAlarmRulesToTable();

private:
    Ui::ConfigView *ui_;
    // TODO: 变电站列表(默认不使用)
    std::vector<StateInfo *> *state_info_vec_;
    DealerHandler *dealer_handler_;
    SearchTreeWidget *search_tree_;
    StateInfo state_info_; ///< 当前选中的变电站
    std::map<int, std::vector<AlarmRule>> station_alarm_rules_map_; ///< 所有变电站的告警规则表
};

#endif // CONFIG_VIEW_H